package com.bkq.cb.BLO;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.bkq.cb.GetDMY;
import com.bkq.cb.SqliteServices;
import com.bkq.cb.DTO.LoanDTO;

public class LoanBLO {
	SqliteServices services;
	private SQLiteDatabase mDB;
	Cursor mCursor;
	GetDMY g = new GetDMY();
	String TAG;
	public LoanBLO(Context context)
	{
		services = new SqliteServices(context);
	}
	
	public boolean insert(LoanDTO obj)
	{
		String sql="insert into tblLoan(idUser, namePerson, dayFrom, dayTo, money, type) values('"+obj.IdUser +"','"+obj.namePerson+"','"+obj.dayFrom+"','"+obj.dayTo+"','"+obj.money+"','"+obj.type+"')";		
		boolean check=false;
		
		try
		{
			this.services.exeSQL(sql);
			check=true;
		}
		catch(Exception ex)
		{
			Log.d(TAG, "Insert  Error");
			
		}
		return check;
	}
	public float[] getValue(String today,String idUser)
	{
		int d;
		int d1=0,d2=0;
		float[]s=new float[8]; 
		s[0] =0;
		s[1] =0;
		s[2] =0;
		s[3]=0;
		s[4]=0;
		s[5]=0;
		//s[6]=0;
		//s[7]=0;
		 String y = g.getYear(today);
		  int m = Integer.parseInt(g.getMonth(today));
		  d = Integer.parseInt(g.getDay(today));
		  switch(m)
		  {
		  case 1:
		  case 3:
		  case 5:
		  case 7:
		  case 8:
		  case 10:
		  case 12:
		  {
			  if(d<=7)
			  {
				  d1 = 1;
				  d2 = 7;
			  }
			  else if(d<=14)
			  {
				  d1 = 8;
				  d2 = 14;
			  }
			  else if(d<=21)
			  {
				  d1 = 15;
				  d2 = 21;
			  }
			  else
			  {
				  d1 = 22;
				  d2 = 31;
			  }
			  break;
		  }
		  case 4:
		  case 6:  
		  case 9:
		  case 11:
		  {
			  if(d<=7)
			  {
				  d1 = 1;
				  d2 = 7;
			  }
			  else if(d<=14)
			  {
				  d1 = 8;
				  d2 = 14;
			  }
			  else if(d<=21)
			  {
				  d1 = 15;
				  d2 = 21;
			  }
			  else
			  {
				  d1 = 22;
				  d2 = 30;
			  }
			  break;
		 }
		  case 2:
		  {
			  if(d<=7)
			  {
				  d1 =1;
				  d2 = 7;
			  }
			  else if(d<=14)
			  {
				  d1 =8;
				  d2 =14;
			  }
			  else if(d<=21)
			  {
				  d1 = 15;
				  d2 = 21;
			  }
			  else
			  {
				  if(Integer.parseInt(y)%4 == 0)
				  {
				  d1 = 22;
				  d2 = 29;
				  }
				  else
				  {
				  d1 = 22;
				  d2 = 28;
				  }
			  }
			  break;
		  }
		  }
		services.openDatabase();
		
		mCursor = services.queryData("select * from tblTransaction");
		    
		     if(mCursor.moveToFirst())
		     {
		    	 
				if(g.getYear(mCursor.getString(3)).equals(y)&&mCursor.getString(1).equals(idUser))
				{
					if(g.getMonth(mCursor.getString(3)).equals(String.valueOf(m)))
					{
					if(Integer.parseInt(g.getDay(mCursor.getString(3))) >= d1 && Integer.parseInt(g.getDay(mCursor.getString(3))) <= d2)
					{
					if(mCursor.getString(5).equals("income"))
					s[0]+=Float.parseFloat(mCursor.getString(4).toString());
					else s[1]+=Float.parseFloat(mCursor.getString(4).toString());
					}
					}
				}
		    	 while(mCursor.moveToNext())
		    	 {
		    		 
						if(g.getYear(mCursor.getString(3)).equals(y)&&mCursor.getString(1).equals(idUser))
						{
							
							if(g.getMonth(mCursor.getString(3)).equals(String.valueOf(m)))
							{
							if(Integer.parseInt(g.getDay(mCursor.getString(3))) >= d1 && Integer.parseInt(g.getDay(mCursor.getString(3))) <= d2)
							{
							if(mCursor.getString(5).equals("income"))
							s[0]+=Float.parseFloat(mCursor.getString(4).toString());
							else
								//s[0]+=Float.parseFloat(mCursor.getString(4).toString());
								s[1]+=Float.parseFloat(mCursor.getString(4).toString());
							
							}
							}
							
						}
						
		    	 }
		    	
		     }
		     s[2] = s[0] - s[1];
	  mCursor.close();
	
		    mCursor = services.queryData("select * from tblLoan");
		    
		   
			if(mCursor.moveToFirst())
			     {
			    	 
					if(g.getYear(mCursor.getString(3)).equals(y)&&mCursor.getString(1).equals(idUser))
					{
						if(g.getMonth(mCursor.getString(3)).equals(String.valueOf(m)))
						{
						if(Integer.parseInt(g.getDay(mCursor.getString(3))) >= d1 && Integer.parseInt(g.getDay(mCursor.getString(3))) <= d2)
						{
						if(mCursor.getString(6).equals("Lend"))
						s[3]+=Float.parseFloat(mCursor.getString(5).toString());
						else s[4]+=Float.parseFloat(mCursor.getString(5).toString());
						}
						}
					}
			    	 while(mCursor.moveToNext())
			    	 {
			    		
			    		 if(g.getYear(mCursor.getString(3)).equals(y)&&mCursor.getString(1).equals(idUser))
							{
								if(g.getMonth(mCursor.getString(3)).equals(String.valueOf(m)))
								{
								if(Integer.parseInt(g.getDay(mCursor.getString(3))) >= d1 && Integer.parseInt(g.getDay(mCursor.getString(3))) <= d2)
								{
								if(mCursor.getString(6).equals("Lend"))
								s[3]+=Float.parseFloat(mCursor.getString(5).toString());
								else s[4]+=Float.parseFloat(mCursor.getString(5).toString());
								}
								}
							}
							
			    	 }
			    	
			     }
			
			s[5]=s[3]-s[4];
			
		mCursor.close();
	  s[6]=d1;
		s[7]=d2;
		    services.close();
		return s;
		
	}

}
